<?php
defined('BY_JYA') or exit('error');
$merchid=$this->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'edit';
if($_RQ['op']=='edit'){
	$return['item']['paytype']=0;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',		
		'edit'=>array(
			array('name'=>'money','title'=>'金额','type'=>'string'),
			array('type'=>'radio','title'=>'收款方式','name'=>'paytype','options'=>'type'),
			array('name'=>'auth_code','title'=>'付款码','type'=>'string','show'=>array('paytype'=>0)),		
			)
	);
	$return['type']=array('0'=>'微信支付','1'=>'现金收款');
	$return['merchid']=$_SESSION['merchid'];
	$return['post']=array('stype'=>0);
	set_html('title','收银台');
	exi($return);
}
if($_RQ['op']=='upfee'){
	$money=$_RQ['money'];
	$mid=$_RQ['mid'];
	if($money<=0){
		 exi('请输入正确金额','error');
	}
	$return=$this->model('cashier')->upfee($money,$merchid,$mid);
	exi($return);
}
if($_RQ['op']=='post'){
	 $postdata=get_postdata();
	 $TIME=date('YmdHi',time());
	 $postdata['money']=$postdata['lastfee'];
	 if($postdata['money']<0){
		 exi('请输入正确金额','error');
	 }
	 if($postdata['paytype']!=1){
		 if(!$postdata['auth_code']){
		  exi('请扫码收款码~','error');
		}
		 $tid='CS'.$TIME.rand(10000,99999);
		$data=array('ordersn'=>$tid,'uniacid'=>$_SESSION['uniacid'],'price'=>$postdata['money'],'createtime'=>time(),'status'=>1);
		$params=array('title'=>'收银台收款','tid'=> $tid,'fee'=>$postdata['money'],'auth_code'=>$postdata['auth_code']);
		$onewechat=pdo_getcolumn('jy_merch_user',array('uniacid' =>$_SESSION['uniacid'],'id'=>$merchid),'onewechat');
		$onewechat=json_decode($onewechat,true);
        if($onewechat && $onewechat['isopen'] && $onewechat['appid'] && $onewechat['wechat_mchid']){
			$config=array(
                 'appid'=>$onewechat['appid'],
                  'mch_id'=>$onewechat['wechat_mchid'],
                  'key'=>$onewechat['wechat_miyue'],
                  'apiclient_cert'=>$onewechat['cert_file']?ROOT_AT.$onewechat['cert_file']:'',
                  'apiclient_key'=>$onewechat['key_file']?ROOT_AT.$onewechat['key_file']:'',
            );	
			$res=cfc('wechat_pay',$config)->micro_pay($params);
		}else{
			$res=cfc('wechat_pay')->micro_pay($params);
		}
		if($res['result_code']=='SUCCESS' && $res['return_code']=='SUCCESS' && $res['total_fee']==$postdata['money']*100 && $tid== $res['out_trade_no']){
			$data=array('ordersn'=>$tid,'uniacid'=>$_SESSION['uniacid'],'price'=>$postdata['money'],'createtime'=>time(),'status'=>1,'paytype'=>0,'paytime'=>$res['time_end'],'payopenid'=>$res['openid']);
			if($postdata['member']){
				$data['mid']=$postdata['member']['id']?:$postdata['member']['mid'];
			}
			pdo_insert('jy_merch_cashier_order',$data); 
			$orderid=pdo_insertid();
			$da=array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid'],'cashierid'=>$merchid,'createtime'=>time(),'money'=>$postdata['money'],'paytype'=>0,'paytime'=>$res['time_end'],'payopenid'=>$res['openid']);
			$da['mid']=$data['mid'];
			pdo_insert('jy_merch_cashier_pay_log',$da); 
			exi('收款成功','','edit'); 
		}else{
			exi('收款失败','error');
		}				
	 }else{
		$data=array('ordersn'=>'CS'.$TIME.rand(10000,99999),'uniacid'=>$_SESSION['uniacid'],'price'=>$postdata['money'],'createtime'=>time(),'status'=>1,'paytime'=>time(),'cashierid'=>$merchid);
		if($postdata['member']){
			$data['mid']=$postdata['member']['id']?:$postdata['member']['mid'];
		}
		pdo_insert('jy_merch_cashier_order',$data); 
		$orderid=pdo_insertid();
		$da=array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid'],'cashierid'=>$merchid,'createtime'=>time(),'money'=>$postdata['money'],'paytype'=>1,'paytime'=>time());
		$da['mid']=$data['mid'];
		pdo_insert('jy_merch_cashier_pay_log',$da);
		exi('收款成功','','edit'); 
	 }
		
}
